#include "sorfttimer_v2.h"
#include "stdio.h"
#include "stdint.h"
#include "stdlib.h"

#define LOG(...) // printf(__VA_ARGS__) /* 日志 */

#define TEST_INTERRUPUT // usleep(500); /* LINUX下模拟多线程和中断，测试定时器会不会异常 FreeRTOS下替换延时函数也可以测试*/

/***********计数值**************/
#define ST_START_COUNT_VALUE 0
#define ST_END_COUNT_VALUE 0
#define ST_ID_NULL 0
/************ID值****************/

#define TIME_WHEEL_CYCLE 10  /* 时间轮总时间片 */

/*******单个定时器结构体 *************/

typedef struct id_menber
{
    uint16_t id;                     /* 软件定时器id */
    uint32_t count;                  /* 计数值 */
    uint32_t target;                 /* 目标值圈数*/
    st_e_state_run_t run_state;      /* 运行时的状态 */
    st_e_state_end_t end_state;      /* 结束后的状态 */
    struct id_menber *st_timer_head; /* 定时器队列 */
    struct id_menber *st_timer_next; /* 定时器队列 */
    st_rtn_func_t rtn_func;          /* 计时结束的回调函数 */
    uint8_t flag;                    /* timer 废弃标志 <1：需要废除> <0：无需废除> 用作断链处理标志 */
} st_id_t;

/************ 时间轮结构体 ******************/
typedef struct time_wheel_node /* 时间轮节点 */
{
    struct time_wheel_node *tw_next; /* 时间轮节点下一个 */
    st_id_t st_head;                 /* 定时器第一个,这个定时器不使用,只用于建链 */
} s_timewheel_node_t;

typedef struct wheel_index /* 滴答指针 */
{
    struct time_wheel_node *tw_index;
    uint32_t cycles;     /* 圈数 */
    uint8_t tw_del_flag; /* 有删除标志 */
    uint8_t tw_new_flag; /* 有新建标志 */
} s_tw_index_t;

/*******所有定时器结构体 *************/
typedef struct timer_id
{
    s_timewheel_node_t timewheel[TIME_WHEEL_CYCLE]; /* 时间轮 */
    st_id_t id_ary[MAX_TIMER_NUM];                  /* 定时器数组 */
} s_timer_id;

#ifdef ST_CLIENT_SERVER_MODE
/*前后台模式，前台运行在main while模式，后台中断定时器设置标志*/
typedef struct timer_event
{
    uint16_t event_id[MAX_TIMER_NUM];
    st_rtn_func_t event_ary[MAX_TIMER_NUM]; /* 数组 */
} s_event_id;
#endif

static s_tw_index_t TW_index; /* time wheel tick index */
static s_timer_id timer_id_ary;

static uint8_t INIT_SORFTIMER_FLG = 0; /* 初始化标志 */

#ifdef ST_CLIENT_SERVER_MODE
static s_event_id event_id_ary;
static void st_add_event_to_list(st_rtn_func_t event_cb, uint16_t id);
#endif

typedef s_timewheel_node_t *ST_POINT_TYPE;

static void st_clear_content(st_id_t *st_index);
static st_id_t *st_find_list_last(st_id_t *st_index);
static st_e_state_crat_t st_put_id_to_list(uint16_t index, uint16_t timer_id, uint32_t count, st_e_state_run_t run_state, st_e_state_end_t end_state, st_rtn_func_t rtn_func);

/**
 * @brief 计算定时器应该插入的位置
 * 返回时间轮位置的指针
 */
static s_timewheel_node_t *st_timewheel_find_timer_index(uint32_t time, uint32_t *cycle)
{
    uint32_t st_tick = 0;           /* tick 数*/
    uint32_t TW_tick = 0;           /*已经走过的tick*/
    uint32_t TW_tick_no_finish = 0; /*已经走过的tick*/

    /*从指针位置到数组头，计算指针走了多少个位置*/
    TW_tick = ((ST_POINT_TYPE)TW_index.tw_index - (ST_POINT_TYPE)&timer_id_ary.timewheel[0]);

    TW_tick_no_finish = TIME_WHEEL_CYCLE - TW_tick;

    time /= TIME_WHEEL_TICK; /*除以tick实际值只保留刻度*/

    *cycle = time / TIME_WHEEL_CYCLE;

    if ((time + TW_tick) >= (TIME_WHEEL_CYCLE))
    {
        time -= TW_tick_no_finish;
    }
    else
    {
        time += TW_tick;
    }

    st_tick = time % TIME_WHEEL_CYCLE;

    if ((st_tick == TW_tick) && (*cycle > 0))
    {
        (*cycle)--;
    }

    return &timer_id_ary.timewheel[st_tick]; /*0-9*/
}

/**
 * @brief 查找定时器列表位置最后一个定时器
 * 返回st位置的指针
 */
static st_id_t *st_find_list_last(st_id_t *st_index)
{
    if (st_index)
    {
        while (st_index->st_timer_next)
        {
            TEST_INTERRUPUT
            st_index = st_index->st_timer_next;
        }
    }

    return st_index;
}

/**
 * @brief 清空指定定时器的内容
 *
 */
static void st_clear_content(st_id_t *index_timer)
{
    index_timer->id = ST_ID_NULL;
    index_timer->count = ST_END_COUNT_VALUE;
    index_timer->target = ST_END_COUNT_VALUE;
    index_timer->run_state = (st_e_state_run_t)TIM_DESTROYED;
    index_timer->end_state = (st_e_state_end_t)TIM_DESTROYED;
    index_timer->rtn_func = NULL;
}

/**
 * @brief 把定时器标记为废弃
 */
static st_e_state_crat_t st_reduce_id_from_list(uint16_t timer_id, uint16_t *index)
{
    uint16_t i = 0;
    uint8_t first_i = 0;
    uint8_t find_flag = 0;

    /*清空index*/
    (*index) = 0;
    /*************查找相同ID或者直接创建****************/
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        /*查找不是废弃 并且ID相同*/
        if ((!timer_id_ary.id_ary[i].flag) && (timer_id_ary.id_ary[i].id == timer_id))
        {
            TEST_INTERRUPUT
            /*判断状态*/
            if (timer_id_ary.id_ary[i].run_state == TIM_LOCK)
            {
                return (st_e_state_crat_t)TIM_LOCK;
            }
            /*如果没有建链，可以重复使用*/
            if (timer_id_ary.id_ary[i].st_timer_head == timer_id_ary.id_ary[i].st_timer_next)
            {
                timer_id_ary.id_ary[i].id = ST_ID_NULL;
                first_i = i;
                find_flag = 1;
                // LOG("!");
                break;
            }

            /*标记为废除*/
            timer_id_ary.id_ary[i].flag = 1;
            TW_index.tw_del_flag = 1;
        }
        /*记录空的位置*/
        if ((!timer_id_ary.id_ary[i].flag) && (timer_id_ary.id_ary[i].id == ST_ID_NULL) && (!find_flag))
        {
            first_i = i;
            find_flag = 1;
            TEST_INTERRUPUT
        }
    }
    if (!find_flag)
    {
        return TIM_CRAT_ERROR;
    }

    (*index) = first_i;

    return TIM_CRAT_SUCES;
}

/**
 * @brief 把定时器找个空位填入
 */
static st_e_state_crat_t st_put_id_to_list(uint16_t index, uint16_t timer_id,
                                           uint32_t count,
                                           st_e_state_run_t run_state,
                                           st_e_state_end_t end_state,
                                           st_rtn_func_t rtn_func)
{
    if ((index < MAX_TIMER_NUM) && (timer_id_ary.id_ary[index].id == ST_ID_NULL))
    {
        timer_id_ary.id_ary[index].id = timer_id;
        timer_id_ary.id_ary[index].count = count;
        timer_id_ary.id_ary[index].target = ST_END_COUNT_VALUE;
        timer_id_ary.id_ary[index].run_state = run_state;
        timer_id_ary.id_ary[index].end_state = end_state;
        timer_id_ary.id_ary[index].rtn_func = rtn_func;
        timer_id_ary.id_ary[index].flag = 0; /*标志为有效*/

        TW_index.tw_new_flag = 1;

        return TIM_CRAT_SUCES; /*返回创建成功的状态*/
    }
    else
    {
        return TIM_CRAT_ERROR;
    }
    return TIM_CRAT_ERROR;
}

/**
 * @brief 检查参数的合理性
 */
static st_e_state_crat_t st_check_id_param(uint16_t timer_id, uint32_t count)
{
    if (timer_id == 0 || count == 0)
    {
        return TIM_CRAT_ERROR;
    }
    if (!INIT_SORFTIMER_FLG)
    {
        st_sorft_timer_init();
    }
    return TIM_CRAT_SUCES;
}

/**
 * @brief 定时器建立链接处理
 */
void st_creat_link_process(void)
{
    uint16_t i = 0;
    uint32_t new_cycle = 0;
    st_id_t *found_timer = NULL;
    st_id_t *last_timer = NULL;
    s_timewheel_node_t *tw_ptr = NULL;

    /*************查找没有建链的ID****************/
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        if ((timer_id_ary.id_ary[i].id != ST_ID_NULL) && (!timer_id_ary.id_ary[i].flag) && (!timer_id_ary.id_ary[i].st_timer_next) && (!timer_id_ary.id_ary[i].st_timer_head))
        {
            found_timer = &timer_id_ary.id_ary[i];
            /*计算定时器插入的tick位置 和 圈数值*/
            tw_ptr = st_timewheel_find_timer_index(found_timer->count, &new_cycle);

            /*从tick位置开始,找到定时器列表最后一个*/
            last_timer = st_find_list_last(&tw_ptr->st_head);

            if (found_timer)
            {
                found_timer->target = new_cycle;

                TEST_INTERRUPUT
                last_timer->st_timer_next = found_timer; /*建立链接*/
                found_timer->st_timer_head = last_timer;
                found_timer->st_timer_next = NULL;
            }
        }
    }
}

/**
 * @brief 定时器断链处理
 */
void st_break_link_process(void)
{

    uint16_t i = 0;

    /*************查找废弃ID****************/
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        /*找到废除标志*/
        if (timer_id_ary.id_ary[i].flag)
        {
            /*断开链接*/
            if (timer_id_ary.id_ary[i].st_timer_head)
            {
                timer_id_ary.id_ary[i].st_timer_head->st_timer_next = timer_id_ary.id_ary[i].st_timer_next;
            }

            /*最后一个位置判断*/
            if (timer_id_ary.id_ary[i].st_timer_next)
            {
                TEST_INTERRUPUT
                timer_id_ary.id_ary[i].st_timer_next->st_timer_head = timer_id_ary.id_ary[i].st_timer_head;
            }

            timer_id_ary.id_ary[i].st_timer_head = NULL;
            timer_id_ary.id_ary[i].st_timer_next = NULL;

            /*清空内容*/
            st_clear_content(&timer_id_ary.id_ary[i]);

            /*清除废弃标志*/
            timer_id_ary.id_ary[i].flag = 0;
            // LOG("c_%d\n", i);
        }
    }
}

/**
 * @brief 定时器结束后的处理
 */
static void st_timer_end_handle(st_id_t *head_timer)
{
    st_id_t end_id = {0};
    st_id_t *this_timer = NULL;

    this_timer = head_timer;

    TEST_INTERRUPUT
    /*while循环先判断左边的条件，先排除空指针风险*/
    while (this_timer != NULL)
    {
        /*定时值不为 0 入口*/
        if ((this_timer->target) && (!this_timer->flag))
        {
            this_timer->target--;

            this_timer = this_timer->st_timer_next; /*后移下一个定时器*/
        }
        /*暂停和结束/废弃的定时器入口*/
        else if ((this_timer->run_state == (st_e_state_run_t)TIM_SUSPEND) || (this_timer->run_state == (st_e_state_run_t)TIM_END) || (this_timer->flag))
        {
            this_timer = this_timer->st_timer_next; /*后移下一个定时器*/
        }
        /*计时结束的定时器*/ /*计时结束*/
        else
        {
            this_timer->target = ST_END_COUNT_VALUE;
            /*拷贝定时器参数*/
            end_id.id = this_timer->id;
            end_id.count = this_timer->count;
            end_id.run_state = this_timer->run_state;
            end_id.end_state = this_timer->end_state;
            end_id.rtn_func = this_timer->rtn_func;
            this_timer->run_state = (st_e_state_run_t)TIM_END;
            TEST_INTERRUPUT
            /*结束定时*/
            if (end_id.end_state == TIM_ONCE || end_id.end_state == TIM_CYCLE)
            {
                /*标记为需要废除需要断链*/
                this_timer->flag = 1;
                /*标记为有删除事件*/
                TW_index.tw_del_flag = 1;
            }

            /*后移下一个定时器*/
            this_timer = this_timer->st_timer_next;

            /*循环触发*/
            if (end_id.end_state == TIM_CYCLE)
            {
                uint16_t index = 0;
                if (!st_reduce_id_from_list(end_id.id, &index))
                {
                    st_put_id_to_list(index, end_id.id, end_id.count, end_id.run_state, end_id.end_state, end_id.rtn_func);
                }
            }
            TEST_INTERRUPUT
            /*执行回调*/
            if (end_id.rtn_func != NULL)
            {
#ifdef ST_CLIENT_SERVER_MODE
                st_add_event_to_list(end_id.rtn_func, end_id.id);
#else
                end_id.rtn_func(end_id.id);
#endif
            }
        }
    }
}

/***********************     public    **************************/
/***********************     public    **************************/
/**
 * @brief 定时器中断回调，对所有计数值进行减少
 *       中断触发为 1ms
 *      根据用户层需要的计时时间,到达就改变状态
 **/
void st_sorft_timer_counter_prosess(void)
{
    /* 此函数放入中断回调*/
    if (INIT_SORFTIMER_FLG)
    {
        TW_index.tw_index = TW_index.tw_index->tw_next; /*tick ++*/

        if (TW_index.tw_index == &timer_id_ary.timewheel[0])
        {
            TW_index.cycles++; /*记录走过的时间*/
        }

        /*处理计时回调任务*/
        if (TW_index.tw_index->st_head.st_timer_next)
        {
            st_timer_end_handle(TW_index.tw_index->st_head.st_timer_next); /*进入定时器处理*/
        }
        /*处理断链任务*/
        if (TW_index.tw_del_flag)
        {
            st_break_link_process();
            TW_index.tw_del_flag = 0;
        }
        /*处理建链任务*/
        if (TW_index.tw_new_flag)
        {
            st_creat_link_process();
            TW_index.tw_new_flag = 0;
        }
    }
}

#ifdef ST_CLIENT_SERVER_MODE
/**
 * @brief 前台调用，处理对应id的事件
 * 后台中断将触发的timer id 标记
 * 此函数负责处理对应的id事件调用
 **/
void st_sorft_timer_event_prosess(void)
{
    uint16_t i = 0;
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        if (event_id_ary.event_ary[i] != NULL)
        {
            event_id_ary.event_ary[i](event_id_ary.event_id[i]);
            event_id_ary.event_id[i] = ST_ID_NULL;
            event_id_ary.event_ary[i] = NULL;
            return;
        }
    }
}
#endif
#ifdef ST_CLIENT_SERVER_MODE
/**
 * @brief 添加事件回调到列表
 **/
static void st_add_event_to_list(st_rtn_func_t event_cb, uint16_t id)
{
    uint16_t i = 0;
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        if (event_id_ary.event_ary[i] == NULL)
        {
            event_id_ary.event_ary[i] = event_cb;
            event_id_ary.event_id[i] = id;
            return;
        }
    }
}
#endif
/**
 @brief 定时器id赋值，使用定时器前先调用一次此接口
**/
void st_sorft_timer_init(void)
{
    uint16_t i = 0;

    /*  清空定时器所有 timer_id = 0*/
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        timer_id_ary.id_ary[i].id = ST_ID_NULL;
        timer_id_ary.id_ary[i].count = ST_END_COUNT_VALUE;
        timer_id_ary.id_ary[i].target = ST_END_COUNT_VALUE;
        timer_id_ary.id_ary[i].run_state = (st_e_state_run_t)TIM_DESTROYED;
        timer_id_ary.id_ary[i].end_state = (st_e_state_end_t)TIM_DESTROYED;
        timer_id_ary.id_ary[i].rtn_func = NULL;
        timer_id_ary.id_ary[i].st_timer_head = NULL;
        timer_id_ary.id_ary[i].st_timer_next = NULL;
        timer_id_ary.id_ary[i].flag = 0;
#ifdef ST_CLIENT_SERVER_MODE
        event_id_ary.event_id[i] = ST_ID_NULL;
        event_id_ary.event_ary[i] = NULL;
#endif
    }

    /*时间轮指针*/
    TW_index.tw_index = &timer_id_ary.timewheel[0]; /*时间轮起始点*/
    TW_index.tw_del_flag = 0;
    TW_index.tw_new_flag = 0;
    /*时间轮初始化*/
    timer_id_ary.timewheel[TIME_WHEEL_CYCLE - 1]
        .tw_next = &timer_id_ary.timewheel[0]; /*首尾相连*/
    timer_id_ary.timewheel[TIME_WHEEL_CYCLE - 1].st_head.st_timer_head = NULL;
    timer_id_ary.timewheel[TIME_WHEEL_CYCLE - 1].st_head.st_timer_next = NULL;

    for (i = 0; i < (TIME_WHEEL_CYCLE - 1); i++)
    {
        timer_id_ary.timewheel[i].tw_next = &timer_id_ary.timewheel[i + 1];
        timer_id_ary.timewheel[i].st_head.st_timer_head = NULL;
        timer_id_ary.timewheel[i].st_head.st_timer_next = NULL;
    }
    INIT_SORFTIMER_FLG = 1;
}

/**
 @brief 启动一个定时器
*       timer_id 用户自定义ID
*       需要计数时间MS
*       计数状态，LOCK_TIMING(计数时无法刷新计数值，只能等到计数结束)
*       计数状态，TIMING 可以中途重新刷新计数值
**/

st_e_state_crat_t st_start_timer(uint16_t timer_id, uint32_t count, st_e_state_run_t run_state)
{
    uint16_t index = 0;

    if (st_check_id_param(timer_id, count))
    {
        return (st_e_state_crat_t)TIM_CRAT_ERROR;
    }

    if (st_reduce_id_from_list(timer_id, &index))
    {
        return (st_e_state_crat_t)TIM_LOCK;
    }

    return (st_e_state_crat_t)st_put_id_to_list(index, timer_id, count, run_state, (st_e_state_end_t)TIM_DESTROYED, NULL);
}
/**
 *@brief 启动一次定时器
 * 计时结束自动销毁定时器
 * **/
st_e_state_crat_t st_start_once_timer(uint16_t timer_id, uint32_t count, st_e_state_run_t run_state)
{
    uint16_t index = 0;
    if (st_check_id_param(timer_id, count))
    {
        return (st_e_state_crat_t)TIM_CRAT_ERROR;
    }

    if (st_reduce_id_from_list(timer_id, &index))
    {
        return (st_e_state_crat_t)TIM_LOCK;
    }

    return (st_e_state_crat_t)st_put_id_to_list(index, timer_id, count, run_state, (st_e_state_end_t)TIM_ONCE, NULL);
}

/**
 * @brief 启动一次定时器
 * 计时结束自动销毁定时器
 **/
st_e_state_crat_t st_start_cb_timer(uint16_t timer_id,
                                    uint32_t count,
                                    st_e_state_run_t run_state,
                                    st_e_state_end_t end_state,
                                    st_rtn_func_t rtn_func)
{
    uint16_t index = 0;
    if (st_check_id_param(timer_id, count))
    {
        return (st_e_state_crat_t)TIM_CRAT_ERROR;
    }

    if (st_reduce_id_from_list(timer_id, &index))
    {
        return (st_e_state_crat_t)TIM_LOCK;
    }

    return (st_e_state_crat_t)st_put_id_to_list(index, timer_id, count, run_state, end_state, rtn_func);
}

/**
 * @brief 关掉定时器，
 * TIM_SUSPEND 定时器不销毁，计数暂停
 * TIM_DESTROYED  销毁定时器，ID将被抹去
 */
st_e_state_close_t st_close_timer(uint16_t timer_id, st_e_state_close_t close_state)
{
    uint16_t i = 0;

    if (timer_id == 0)
    {
        return (st_e_state_close_t)TIM_CRAT_ERROR;
    }
    if (!INIT_SORFTIMER_FLG)
    {
        st_sorft_timer_init();
    }
    /*************查找相同ID或者直接创建****************/
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        if ((!timer_id_ary.id_ary[i].flag) && (timer_id_ary.id_ary[i].id == timer_id))
        {
            TEST_INTERRUPUT
            if (close_state == TIM_SUSPEND)
            {
                timer_id_ary.id_ary[i].run_state = (st_e_state_run_t)close_state;
                timer_id_ary.id_ary[i].end_state = (st_e_state_end_t)close_state;
                return TIM_SUSPEND;
            }
            else if (close_state == TIM_DESTROYED)
            {
                /*标记为废除*/
                timer_id_ary.id_ary[i].flag = 1;
                timer_id_ary.id_ary[i].run_state = (st_e_state_run_t)close_state;
                timer_id_ary.id_ary[i].end_state = (st_e_state_end_t)close_state;
                TW_index.tw_del_flag = 1;
                return TIM_DESTROYED;
            }
            break;
        }
    }

    return TIM_DESTROYED;
}

/**
 *@bref 返回定时器状态
 有此ID的定时器就返回当前状态
 没有就返回DESTROYED
 * **/
st_e_state_run_t st_timer_state(uint16_t timer_id)
{
    uint16_t i = 0;
    if (timer_id == 0)
    {
        return (st_e_state_run_t)TIM_DESTROYED;
    }
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        if ((timer_id_ary.id_ary[i].id) == timer_id && (!timer_id_ary.id_ary[i].flag))
        {
            return timer_id_ary.id_ary[i].run_state;
        }
    }
    return (st_e_state_run_t)TIM_DESTROYED;
}

/*
 @bref 查询计数值
*/
uint32_t st_timer_count_value(uint16_t timer_id)
{
    uint16_t i = 0;
    st_id_t *found_timer = NULL;

    if (timer_id == 0)
    {
        return ST_END_COUNT_VALUE;
    }
    if (!INIT_SORFTIMER_FLG)
    {
        st_sorft_timer_init();
    }
    /*************查找相同ID****************/
    for (i = 0; i < MAX_TIMER_NUM; i++)
    {
        if ((!timer_id_ary.id_ary[i].flag) && (timer_id_ary.id_ary[i].id == timer_id))
        {
            TEST_INTERRUPUT
            found_timer = &timer_id_ary.id_ary[i];
            break;
        }
    }

    if (found_timer && (found_timer->target != ST_END_COUNT_VALUE))
    {
        return ((found_timer->target + 1) * TIME_WHEEL_CYCLE); /*舍弃一部分定时值，获取轮节点太复杂耗时*/
    }

    return ST_END_COUNT_VALUE;
}

/**
 * @brief 获取软件定时器所运行的总时间
 */
uint32_t st_get_all_runtime(void)
{
    return (TW_index.cycles * TIME_WHEEL_CYCLE) + (((ST_POINT_TYPE)TW_index.tw_index - (ST_POINT_TYPE)&timer_id_ary.timewheel[0]));
}

/**
 * @brief 打印时间轮的结构关系
 * 进入这个函数必须进行线程保护或中断关闭或原子操作等等，防止错误
 */
void st_print_time_wheel(void)
{

    st_id_t *st_ptr = NULL;
    s_timewheel_node_t *tw_ptr = NULL;

    if (!INIT_SORFTIMER_FLG)
    {
        st_sorft_timer_init();
    }

    tw_ptr = &timer_id_ary.timewheel[0];

    /*************查找相同ID或者直接创建****************/
    LOG("-----------wheel node size: %d \n", (int)sizeof(s_timewheel_node_t));

    do
    {
        if (TW_index.tw_index == tw_ptr)
        {
            LOG("->");
        }
        LOG(" Wheel index:%ld ->", ((ST_POINT_TYPE)tw_ptr - (ST_POINT_TYPE)&timer_id_ary.timewheel[0]));

        if (tw_ptr->st_head.st_timer_next)
        {
            st_ptr = tw_ptr->st_head.st_timer_next;
            do
            {
                LOG("(timer:%d :<%d> <%p> -> %p) ", st_ptr->id, st_ptr->target, st_ptr, st_ptr->st_timer_next);
                st_ptr = st_ptr->st_timer_next;
            } while (st_ptr);
        }

        tw_ptr = tw_ptr->tw_next; /*tick 移位*/
        LOG("\r\n");
    } while (tw_ptr != &timer_id_ary.timewheel[0]);

    LOG("\r\n\r\n");
}
